package frame.yuangong;

import MyBatis.MyBatislianjian;
import frame.LandPanel;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.EmployeeMapper;
import org.example.pojo.Employee;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;
import frame.yuangongPabel;


public class yuangonggerxinxi extends JInternalFrame {
    private JTextArea textArea;
    public SqlSession sqlSession = MyBatislianjian.getSqlSession();
    public yuangonggerxinxi() {
        super("个人信息", true, true, true, true);
        this.setLayout(new BorderLayout());
        this.setSize(500, 400); // 增大窗口尺寸

        // 中央面板
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 设置边距
        centerPanel.add(Box.createVerticalGlue()); // 添加垂直间距

        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setFont(new Font("SansSerif", Font.PLAIN, 16)); // 设置更大的字体
        textArea.setLineWrap(true); // 自动换行
        textArea.setWrapStyleWord(true); // 断行不断字
        textArea.setMargin(new Insets(10, 10, 10, 10)); // 设置内边距

        // 创建滚动面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(400, 250)); // 增大首选尺寸
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); // 设置边框

        centerPanel.add(scrollPane);
        centerPanel.add(Box.createVerticalGlue()); // 添加垂直间距

        this.add(centerPanel, BorderLayout.CENTER);

        // 加载员工信息
        loadEmployeeInfo();
    }

    private void loadEmployeeInfo() {
        String eid = LandPanel.getyonghu1();
        // 根据员工编号查询员工信息// 这里假设 sqlSession 是从 yuangongPabel 中获取的
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.selectEmployeeByEid(eid);

        if (employee != null) {
            // 显示员工信息
            displayEmployeeInfo(employee);
        } else {
            textArea.setText("未找到员工信息");
        }
    }

    private void displayEmployeeInfo(Employee employee) {
        // 显示员工信息到文本区域
        StringBuilder sb = new StringBuilder();
        sb.append("ID: ").append(employee.getEid()).append("\n")
                .append("姓名: ").append(employee.getEName()).append("\n")
                .append("密码: ").append(employee.getEPas()).append("\n")
                .append("职位: ").append(employee.getElevel()).append("\n")
                .append("电话: ").append(employee.getEtelPhone()).append("\n")
                .append("工资: ").append(employee.getESalary()).append("\n")
                .append("其他信息: ").append(employee.getOther());

        textArea.setText(sb.toString());
    }
}
